package ch07_GUI编程;

import javax.swing.*;
import java.awt.*;

public class JRadioButtonDemo {
	public static void main(String[] args) {
		BaseFrame f = new BaseFrame("JRadioButton Demo");
		f.setLayout(new GridLayout(5, 1));

		ButtonGroup sexGroup = new ButtonGroup();
		ButtonGroup majorGroup = new ButtonGroup();

		String[] texts = { "男", "女", "英语", "计算机", "数学" };
		JRadioButton[] rbs =new JRadioButton[texts.length];
		ImageIcon unchecked =ImageFactory.create("unchecked.png");
		ImageIcon checked= ImageFactory.create("checked.png");
		for (int i = 0; i < rbs.length; i++) {
			rbs[i]=new JRadioButton(texts[i]);
			if(i>=2) {
				rbs[i].setIcon(unchecked);
				rbs[i].setSelectedIcon(checked);
			}
			f.add(rbs[i]);
		}
		sexGroup.add(rbs[0]);
		sexGroup.add(rbs[1]);
		majorGroup.add(rbs[2]);
		majorGroup.add(rbs[3]);
		majorGroup.add(rbs[4]);

		rbs[2].setSelected(true);  /* 即使设置两个选中，也只能选中一个； */
		rbs[3].setSelected(true);
		f.showMe();
	}
}
